import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: JsonMap
 * @Description:
 * @Author: ZengFz
 * @Date: 2024/4/30 14:43:56
 */
public class JsonMap {
    public static void main(String[] args) {
        LinkedHashMap<String, Object> zhangsan = new LinkedHashMap<>();
        zhangsan.put("name", "张三");
        zhangsan.put("age", 25);

        LinkedHashMap<String, Object> address = new LinkedHashMap<>();
        address.put("city", "上海");
        address.put("state", "奉贤");
        address.put("zip", "10001");
        zhangsan.put("address", address);

        List<Map<String, Object>> friends = new ArrayList<>();

        LinkedHashMap<String, Object> lisi = new LinkedHashMap<>();
        lisi.put("name", "李四");
        lisi.put("age", 27);
        LinkedHashMap<String, Object> lisiAddress = new LinkedHashMap<>();
        lisiAddress.put("city", "上海");
        lisiAddress.put("state", "虹桥");
        lisiAddress.put("zip", "10002");
        lisi.put("address", lisiAddress);
        friends.add(lisi);

        LinkedHashMap<String, Object> wangwu = new LinkedHashMap<>();
        wangwu.put("name", "王五");
        wangwu.put("age", 24);
        LinkedHashMap<String, Object> wangwuAddress = new LinkedHashMap<>();
        wangwuAddress.put("city", "杭州");
        wangwuAddress.put("state", "西湖");
        wangwuAddress.put("zip", "20001");
        wangwu.put("address", wangwuAddress);
        friends.add(wangwu);

        zhangsan.put("friends", friends);

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String jsonString = gson.toJson(zhangsan);
        System.out.println(jsonString);
    }
}



